﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Core.FrameWork;
using Core.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Core.Common;
using Senparc.NeuChar.Entities;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.MessageHandlers;


namespace Core.Web.Controllers
{


    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private DataDBContext _dbContext;
        private readonly string access_token = "34_cc3M2t79WW7PQXDsJvWGpRtk1yxkVfLugwBIYuWs63AiE5fWnt49Fwb5etDJAHmOqKoZUzmKThPV6Q5t6OJ8NIP44A_JzhhR0o_56AOxwH8YypWVVxQP2egPajRpAfMt2QY6tsnNUolDitSqDBUjABAJHA";

        public HomeController(ILogger<HomeController> logger, DataDBContext dbContext)
        {
            _logger = logger;
            _dbContext = dbContext;
        }

        #region Distinct重写去重优化
        public Task<object> Index()
        {

            //string ticket = String.Empty;
            //if (string.IsNullOrEmpty(ticket))
            //{
            //    var result = WXHelper.GetAccessTokenAsync().Result;
            //    if (result.errcode == Senparc.Weixin.ReturnCode.请求成功)
            //    {
            //        ticket = result.ticket;
            //    }
            //}

            //string qrUrl = Senparc.Weixin.MP.AdvancedAPIs.QrCodeApi.GetShowQrCodeUrl(ticket);

            //var list = _dbContext.Person.ToList();
            //var time1 = Time(() =>
            //{
            //    list.GroupBy(d => new { d.Age, d.Name })
            //        .Select(d => d.FirstOrDefault())
            //        .ToList();
            //});

            //var time2 = Time(() =>
            //{
            //    list.Distinct(d => new { d.Age, d.Name }).ToList();
            //});

            //var time3 = Time(() =>
            //{
            //    list.Distinct((a, b) => a.Age == b.Age && a.Name == b.Name).ToList();
            //});

            //ViewBag.Time = string.Format("分组耗时:{0}--HashSet耗时:{1}--委托耗时:{2}", time1, time2, time3);
            //ViewBag.Code = qrUrl;
            return Task.Run(()=>(object)"");
        }




        private static long Time(Action action)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            action();
            stopwatch.Stop();
            return stopwatch.ElapsedMilliseconds;
        }

        public IActionResult Privacy()
        {
            var list = new List<Person>();
            for (int i = 0; i < 1000000; i++)
            {

                list.Add(new Person() { Age = 18, Name = "李阳" });
            }
            try
            {
                var task = _dbContext.Person.AddRangeAsync(list);
                var x = _dbContext.SaveChangesAsync();

            }
            catch (Exception e)
            {
                throw new Exception("错误：" + e.Message);
            }
            return View();
        }
        #endregion 

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new Models.ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
